dojo.provide("com.ibm.acComboBox");

dojo.require("dijit.form.ComboBox");

dojo.declare("com.ibm.acComboBox", dijit.form.ComboBox, {
    _onKeyPress: function(evt){
        var pw = this._popupWidget;
        var dk = dojo.keys;
		//If ',' is pressed list all the contractors.
        if (evt.charCode == 44) {
            this._startSearch(""); 
        }
        else  
            if (evt.keyCode == dojo.keys.ENTER) {
                var pw = this._popupWidget;
                var highlighted;
				//If Enter is pressed, append the selected one to the input box.
                if (this._isShowingNow && (highlighted = pw.getHighlightedOption())) {
                    var fn = this.focusNode;
                    var itemlabel = highlighted.textContent;
                    fn.value = fn.value.substring(0, fn.value.lastIndexOf(',') + 1).concat(itemlabel);
                    dijit.selectInputText(fn, fn.value.length);
                    this._hideResultList();
                }
                else {
                    this.setDisplayedValue(this.getDisplayedValue());
                }
                evt.preventDefault();
            
            }
            else {
                this.inherited(arguments);
            }
    },
    _announceOption: function(/*Node*/node){
        if (node == null) {
            return;
        }
        var newValue;
        if (node == this._popupWidget.nextButton ||
        node == this._popupWidget.previousButton) {
            newValue = node.innerHTML;
        }
        else {
            newValue = this.store.getValue(node.item, this.searchAttr);
        }
        this.focusNode.value = this.focusNode.value.substring(0, this._getCaretPos(this.focusNode));
        dijit.setWaiState(this.focusNode, "activedescendant", dojo.attr(node, "id"));
		//If there is a ',' in the string, use the customized autocomplete function.
        var hasComma = this.focusNode.value.lastIndexOf(',') <= this.focusNode.value.length - 1;
        if (hasComma && this.focusNode.value.length != 0) {
            this._autoCompleteTextAfterComma(newValue);
        }
        else {
            this._autoCompleteText(newValue);
        }
    },
    _autoCompleteTextAfterComma: function(/*String*/text){
    
        var fn = this.focusNode;
        var index = this.focusNode.value.lastIndexOf(',') < 0 ? 0 : this.focusNode.value.lastIndexOf(',');
        dijit.selectInputText(fn, fn.value.length);
        if (index != fn.value.length - 1) {
            return;
        }
        fn.value = fn.value.concat(text);
        dijit.selectInputText(fn, index + 1);
        
    },
    _startSearchFromInput: function(){
        if (this.focusNode.value.lastIndexOf(',') < 0) {
            this._startSearch(this.focusNode.value);
        }
        else {
            this._startSearch(this.focusNode.value.substring(this.focusNode.value.lastIndexOf(',') + 1, this.focusNode.value.length))
        }
    },
    _doSelect: function(tgt){
        this.item = tgt.item;
        if (this.focusNode.value.lastIndexOf(',') < 0) {
            this.setValue(this.store.getValue(tgt.item, this.searchAttr), true);
        }
        else {
            var value = this.focusNode.value.substring(0,this.focusNode.value.lastIndexOf(',') + 1).concat(this.store.getValue(tgt.item, this.searchAttr));
			this.setValue(value,true);
        }
    }
    
    
});
